﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Handrise.CDCable.Mobile.Business;


namespace Handrise.CDCable.Mobile.UI
{
    public partial class FrmPatrolPoint : FrmBase
    {
        private PlanDevice planDevice;

        private PatrolItemService patrolItemService = new PatrolItemService();

        private PatrolInfoService patrolInfoService = new PatrolInfoService();

        private List<PatrolDetail> patrolDetails;

        private string workType;

        private string parentForm;

        public FrmPatrolPoint()
        {
            InitializeComponent();
            ttlbrhead.Text = "设备巡视";
            ilstbcup.Images.Add(ResourceImage.缺陷01);
            ilstbcup.Images.Add(ResourceImage.空白);
            ilstbcup.Images.Add(ResourceImage.返回01);
            ilstdown.Images.Add(ResourceImage.缺陷02);
            ilstdown.Images.Add(ResourceImage.空白);
            ilstdown.Images.Add(ResourceImage.返回02);
            btnPrior.UpImage = ResourceImage.上一页可点击;
            btnPrior.DownImage = ResourceImage.上一页点击;
            btnPrior.DisableImage = ResourceImage.上一页灰;
            btnNext.UpImage = ResourceImage.下一页可点击;
            btnNext.DownImage = ResourceImage.下一页点击;
            btnNext.DisableImage = ResourceImage.下一页灰;
        }

        private void FrmPatrolPoint_Load(object sender, EventArgs e)
        {
            if (IsReturn)
                LoadCacheData();
            else
                LoadData();

            //获取巡视详情
            patrolDetails = patrolInfoService.GetPatrolDetailByPlanDeviceID(planDevice.PlanDeviceID);

            //获取巡视要点
            List<PatrolItem> patrolItems = patrolItemService.GetPatrolItemsByType(workType);
            dlPatrolItem.DataSource = patrolItems;
            dlPatrolItem.DataBind();

            tbTitle.Text = planDevice.DeviceName;

            SetButtonState();

        }

        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadData()
        {
            parentForm = (string)Parameter[0];
            planDevice = (PlanDevice)Parameter[1];
            workType = (string)Parameter[2];
        }

        /// <summary>
        /// 加载缓存数据
        /// </summary>
        private void LoadCacheData()
        {
            parentForm = (string)Caches[0];
            planDevice = (PlanDevice)Caches[1];
            workType = (string)Caches[2];
        }

        private void bcxunjian_OnButtonClick(object sender, int buttonIndex)
        {
            switch (buttonIndex)
            {
                case 0: //缺陷管理
                    FormManage.GotoNext("FrmFlawList", new object[] { "FrmPatrolPoint", planDevice }, new object[] { parentForm, planDevice, workType }, false);
                    break;
                case 2:
                    FormManage.GotoNext(parentForm,null,null, true);
                    break;
            }
        }



        private void dlsttask_OnItemClick(HandRise.MBHOP.Controls.DataListItem item)
        {
            PatrolItem patrolItem = (PatrolItem)item.DataSource;
            PatrolDetail patrolDetail = patrolDetails.Where(t => t.PatrolItemID == patrolItem.PatrolItemID).FirstOrDefault();
            FormManage.GotoNext("FrmPatrolException", new object[] { item.DataSource, planDevice, patrolDetail }, new object[] {parentForm, planDevice, workType }, false);
        }

        private void dlsttask_OnItemCreateBefore(HandRise.MBHOP.Controls.DataListItem item)
        {
            PatrolItem patrolItem = (PatrolItem)item.DataSource ;

            PatrolDetail patrolDetail = patrolDetails.Where(t => t.PatrolItemID == patrolItem.PatrolItemID).FirstOrDefault();
            if (patrolDetail == null)
            {
                item.IconIndex = 0;
            }
            else
            {
                item.IconIndex = 1;
            }
        }

        private void ibpagenext_Click(object sender, EventArgs e)
        {
            dlPatrolItem.PreviousPage();
            SetButtonState();
        }

        private void ibpageprev_Click(object sender, EventArgs e)
        {
            dlPatrolItem.NextPage();
            SetButtonState();
        }

        private void SetButtonState()
        {
            btnNext.Enabled = dlPatrolItem.PageCount > 0 && dlPatrolItem.PageIndex < dlPatrolItem.PageCount;
            btnPrior.Enabled = dlPatrolItem.PageCount > 0 && dlPatrolItem.PageIndex > 1;
        }
    }
}